package com.jian.mall.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.jian.mall.common.redis.User;
import com.jian.mall.common.utils.R;
import com.jian.mall.order.feign.CartFeign;
import com.jian.mall.order.feign.MemberFeign;
import com.jian.mall.order.interceptor.OrderInterceptor;
import com.jian.mall.order.vo.CartItem;
import com.jian.mall.order.vo.ConfirmVo;
import com.jian.mall.order.vo.MemberReceiveAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jian.mall.common.utils.PageUtils;
import com.jian.mall.common.utils.Query;

import com.jian.mall.order.dao.OrderDao;
import com.jian.mall.order.entity.OrderEntity;
import com.jian.mall.order.service.OrderService;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    private MemberFeign memberFeign;
    @Autowired
    private CartFeign cartFeign;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public ConfirmVo confirmInfo() {
        User user = OrderInterceptor.userThreadLocal.get();
        ConfirmVo confirmVo = new ConfirmVo();
        // 查询地址
        R r = memberFeign.addressByUserId(user.getId());
        if (r != null && r.getCode() == 0) {
            List<MemberReceiveAddress> list = new ArrayList<>();
            list = r.get("address", list.getClass());
            confirmVo.setMemberReceiveAddresses(list);
        }


        // 查询选中物品
        R r1 = cartFeign.getSelective();
        if (r1 != null && r1.getCode() == 0) {
            List<CartItem> cartItems = new ArrayList<>();
            cartItems = r1.get("items", cartItems.getClass());
            System.out.println(cartItems);
            confirmVo.setCartItems(cartItems);
        }

        return confirmVo;
    }

}